package com.yuanfeng.base.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.base.dto.BasePlatformBrankDTO;
import com.yuanfeng.base.entity.BasePlatformBrankEntity;
import com.yuanfeng.base.mapper.BasePlatformBrankMapper;
import com.yuanfeng.base.service.BasePlatformBrankService;
import com.yuanfeng.commoms.dto.userms.UserTokenBeanDTO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.springframework.stereotype.Service;

import java.util.Base64;

/**
 * @ClassName BasePlatformBrankServiceImpl
 * @Description  平台收款银行卡表
 * @Author cyp
 * @Date 2023-07-03 09:56:25
 * @Version 1.0
 **/
@Service
public class BasePlatformBrankServiceImpl extends ServiceImpl<BasePlatformBrankMapper, BasePlatformBrankEntity>
        implements BasePlatformBrankService {


    @Override
    public ResponseResult queryBrank(UserTokenBeanDTO param) {
        BasePlatformBrankEntity platformBrankPo = this.baseMapper.queryBrank();
        return ResponseResult.success(platformBrankPo);
    }

    @Override
    public ResponseResult updateBrank(BasePlatformBrankDTO basePlatformBrankDto) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(basePlatformBrankDto.getToken());
        if (null != userInfo) {
            // 更新或添加银行卡信息
            this.baseMapper.insertBrank(basePlatformBrankDto);
            return ResponseResult.success(1,"操作成功");
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }

    @Override
    public ResponseResult queryBrankEncryption(UserTokenBeanDTO param) {
        String encodedString = null;
        BasePlatformBrankEntity platformBrankPo = this.baseMapper.selectOne(new LambdaQueryWrapper<BasePlatformBrankEntity>());
        if (null != platformBrankPo) {
            //base64加密返
            encodedString = Base64.getEncoder().encodeToString(JSONObject.toJSONString(platformBrankPo).getBytes());
            return ResponseResult.success(encodedString);
        }
        return ResponseResult.success();
    }
}
